package net.entelijan.cobean.examples.showcase.tree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ISingleSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.tree.DefaultTreeLevelDescription;
import net.entelijan.cobean.tree.ListTreeInitializer;
import net.entelijan.cobean.tree.SimpleIconMapper;
import net.entelijan.scf.IShowcase;

public class ListTreeShowcase implements IShowcase<DefaultTreePanel> {

	private ILitService service = null;
	private ISingleSelectionListCobean<Object> cobean;

	public ListTreeShowcase() {
		super();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void init(final DefaultTreePanel panel) {

		// Create an initializer
		ListTreeInitializer ini = new ListTreeInitializer();
		ini.setCoTree(panel.getTree());
		ini.setCoCollapseAllButton(panel.getCollapseAllButton());
		ini.setRowHeight(32);

		// Create the descriptors for each level
		DefaultTreeLevelDescription<LitBook> bookDescription = new DefaultTreeLevelDescription<LitBook>();
		bookDescription.setColumnDescs(ColumnDesc
				.createPropertyOnlyColumnDescs("title", "year"));
		bookDescription.setIconMapper(new SimpleIconMapper<LitBook>(
				"icons/book-01-32.png"));

		DefaultTreeLevelDescription<LitAuthor> authorDescription = new DefaultTreeLevelDescription<LitAuthor>();
		authorDescription.setChildrenMethodName("getBooks");
		authorDescription.setColumnDescs(ColumnDesc
				.createPropertyOnlyColumnDescs("name", "country"));
		authorDescription.setIconMapper(new SimpleIconMapper<LitAuthor>(
				"icons/author-01-32.png"));

		authorDescription.setChildDescription(bookDescription);

		ini.setTreeLevelDescription(authorDescription);

		cobean = ini.initialize();
		final List authors = ExamplesUtil.filterAuthorsWithBooks(service
				.loadAllAuthors());
		cobean.setValue(authors);

		// Now the table displays some data

		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, cobean.getSelectedValue());
			}

		});

		panel.getSelectRandomItemButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						cobean.setSelectedValue(getRandomItem(authors));
					}

				});

	}

	private Object getRandomItem(List<LitAuthor> authors) {
		Object re;
		Random ran = new Random();
		if (ran.nextBoolean()) {
			re = authors.get(ran.nextInt(authors.size()));
		} else {
			LitAuthor ranAuth = authors.get(ran.nextInt(authors.size()));
			List<LitBook> books = ranAuth.getBooks();
			re = books.get(ran.nextInt(books.size()));
		}
		return re;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

	public ISingleSelectionListCobean<Object> getCobean() {
		return cobean;
	}

	public void setCobean(ISingleSelectionListCobean<Object> cobean) {
		this.cobean = cobean;
	}

}
